package com.zxp.wx.mp.interceptor;

import cn.hutool.core.util.StrUtil;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zxp
 * @date 2021/8/11 11:48
 */
public class WxOauth2LoginInterceptor implements HandlerInterceptor{

    /**
     * 通过微信浏览器来访问的,并且没有通过授权登陆,则拦截跳到授权页面
      */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userAgent = request.getHeader( "User-Agent" );

        // 如果不是微信浏览器，就放行
        if(StrUtil.isBlank(userAgent) || !StrUtil.containsAnyIgnoreCase(userAgent,"micromessenger") ){
            return true;
        }
            

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
